#include "sqlite.hpp"
#include <cassert>
#include <vector>

int sel_stu_cb(void* arg, int col_count, char** result, char** fields_name)
{
    // 每行数据都要调用此函数进行处理
    std::vector<std::string>* ret = (std::vector<std::string>*)arg;
    ret->push_back(result[1]);
    return 0; // 这个一定要写，不然会触发 ABORT
}

int main()
{
    SqliteHelper helper("./test.db");
    // 1.创建/打开数据库
    assert(helper.open());

    // 2.创建表
    const char* sql_create = "create table if not exists student(sn int primary key, name varchar(30), age int);";
    assert(helper.exec(sql_create, nullptr, nullptr));

    // 3.插入，修改，删除，查询
    // const char* sql_insert = "insert into student values(1, '小明', 18), (2, '小红' , 17), (3, '小刚', 19)";
    // assert(helper.exec(sql_insert, nullptr, nullptr));

    // const char* sql_update = "update student set name='张小明' where sn=1;";
    // assert(helper.exec(sql_update, nullptr, nullptr));

    // const char* sql_delete = "delete from student where sn=3;";
    // assert(helper.exec(sql_delete, nullptr, nullptr));

    const char* sql_select = "select * from student;";
    std::vector<std::string> ret; // 存储查询结果中的所有姓名
    helper.exec(sql_select, sel_stu_cb, &ret);
    for (auto& e : ret)
        std::cout << e << std::endl;
    // 4.关闭数据库
    helper.close();
    return 0;
}